/**
 * 党务公开初始化模块，提供党务公开编辑页面初始化组件功能
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","ajaxServ","ckeditor-jquery","deptUserSelect","valueUtil","uploadfiles","croppic"],
		function($,sysmanMain,ajaxServ,ckeditor,deptUserSelect,valueUtil,uploadfiles,croppic) {
			
		'use strict';
		var contentsid = 'contents',lob_contentsid = 'lob_contents',
			contentEl,lob_contentEl,
			editor1,
			uploadListurl = '/upload/query/getListDomain.action',
			imageListurl = '/upload/query/getImageListDomain.action',
			uploadurl='/upload/uploadCkeditor.action',
			
			//业务代码
			bizcode = 'goodDoctorNurse',
			
			imageUploadurl = '/upload/uploadCkeditorImage.action'
		var public_method ={
			
			
			
			rowClick:function(index,data){
				
				if(window.console)
					console.info('inpatient rowClick event fired ...')
				var _config = sysmanMain.getModuConf('inpatientItemFee');
				if(_config ==null){
					_config = {}					
				}	
				
				_config.initQueryParams = {field:'inpatient_no',fieldVal:data.inpatient_no}
				sysmanMain.setModuConf('inpatientItemFee',_config)
				sysmanMain.medicalInpatientItemFeeSelect()
			},
			/**
			 * 初始化新增页面
			 * @param _options.record 列表选中行
			 * @param _options.edit_window 页面form对象
			 * @param _options.rootId 页面根节点
			 */
			initElement:function(_options){
				
				if(window.console)
					console.info('party edit loaded and starting ...')
				var _rootpath = '/'+sysmanMain.getRootPath()
				
				var lobQueryUrl='/lob/getOne.action'
				
				//初始化bizcode
				$('#bizcode').val(bizcode);
				
				
				var basepath =  window.location.pathname +'ckedtiro'
				var _tmp = window.CKEDITOR
				if(window.CKEDITOR_BASEPATH){
					
				}else{
					
					window.CKEDITOR_BASEPATH = basepath
				}
				
				//人员选择
				var edit_window_id = _options.edit_window
				var _el = $('#'+edit_window_id+' #public_user_name')
				if(_el &&_el.length>0){
					
					$(_el).textbox({
						onClickButton:function(data){
							var _userParam = {
								userids:edit_window_id +' #public_user_id',
								usernames:edit_window_id+' #public_user_name',
								deptid:edit_window_id +' #public_dept_id',
								deptname:edit_window_id+' #public_dept_name',
								selectByOwner:true,
								subsystem:'document'
							}
							deptUserSelect.openPage(_userParam)							
						}
					})
					
					var txtValue = $(_el).textbox('getValue')
					if(!txtValue || $.trim(txtValue)==''){
						var usr = sysmanMain.getCurrentUser();
						$(_el).textbox('setValue',usr.name)
						$('#'+edit_window_id+' #public_user_id').val(usr.id)
						
						$('#'+edit_window_id+' #public_dept_name').textbox('setValue',usr.deptName)
						$('#'+edit_window_id+' #public_dept_id').val(usr.deptid)
					}
				}
				
				//时间自动赋值
				var edit_window_id = _options.edit_window
				_el = $('#'+edit_window_id+' #public_date')
				if(_el &&_el.length>0){
					var oldValue = $(_el).datebox('getValue');
					if(!oldValue){
						var curDate = new Date();
						var _tmp = valueUtil.getDateValue({source:curDate,format:'yyyy-MM-dd HH:mi:ss'})
						$(_el).datetimebox('setValue',_tmp);
					}
				}
				
				
				
				
				
				//判断id值是否存在
				var _id
				_el = $('#'+edit_window_id+' #id')
				if(!_el.val()){
					var _date = new Date();
					_id = 'TMP' + bizcode + _date.getFullYear()+''+(_date.getMonth()+1)+''+_date.getDate()+'' +_date.getMilliseconds();
					_el.val(_id);
					$('#tmpid').val(_id);
				}else{
					_id = _el.val();
				}
				
				//判断图片dom对象是否存在
				var _cropperId = 'img-face';
				if($('#'+_cropperId).length>=1){
					
					var _reset = false;
					var _cropperOptions = {
						uploadUrl: '/'+sysmanMain.getRootPath() + '/upload/uploadPic.action',
						imgEyecandy:false,
						doubleZoomControls:false,
			    		rotateControls: false,
						uploadData	: {bizno:_id , bizcode: bizcode},
						onBeforeImgUpload: function(){ console.log('onBeforeImgUpload') },
						onAfterImgUpload: function(t){ 
							console.log('onAfterImgUpload') ;
							if(t&& t.id){
								//更新picid
								$('#picid').val(t.id);
							}						
						},
						onImgDrag: function(){ console.log('onImgDrag') },
						onImgZoom: function(){ console.log('onImgZoom') },
						onBeforeImgCrop: function(){ console.log('onBeforeImgCrop') },
						onAfterImgCrop:function(){ console.log('onAfterImgCrop') },
						onReset:function(){ 
							cropperHeader.destroy();
							_cropperOptions.loadPicture = null;
							cropperHeader = new Croppic(_cropperId, _cropperOptions);
						},
						onError:function(errormessage){ console.log('onError:'+errormessage) }
					}
					
					//判断是否预加载图片
					var _picid = $('#picid').val();
					if(_picid){
						_cropperOptions.loadPicture = '/portal/getImage.action?id=' + _picid;
					}
					var cropperHeader = new Croppic(_cropperId, _cropperOptions);
				}
				
				
			},
			baseFileBut:function(_options){
				//判断form对象是否存在
				if(_options && _options.rootId){
					var _table = $('#' + _options.rootId + ' table.dg-detail')
					if(_table.length==1){
						$(_table).datagrid({
							onClickCell:private_method.clickCell
						})
					}
				}
			},
			/**
			 * 提交前执行的数据处理函数
			 */
			beforeSubmit:function(){
				
				var _data = {
					duties_name : $('#duties_name').val(),
					picid		: $('#picid').val(),
					elect_year	: $('#elect_year').val()
				}
				
				var _id = $('#id').val()
				
				if(_id && _id.substring(0,3)=='TMP'){
					$('#id').val('');
				}
				return {form_content:$.toJSON(_data)}
			},
			
			onClose:function(){
				if(editor1){
					editor1.destroy()
					editor1 = null
				}
				
			}
			
		}
		
		var private_method ={
			
						
			clickCell:function(index,field,value){
				if(window.console)
					console.info(' clickCell fired ...')
				
				if(field =='arc_num'){
					var $bizgrid= $('#'+'doc-docdetail' + ' table.dg-detail')
					var record = $($bizgrid).datagrid('getRows')[index];				
					var _moduleConfig = sysmanMain.getCurrentModuConfig();
					_moduleConfig.moduleCode = record.bizcode;
					var rootPath = '/' +sysmanMain.getRootPath();
					var bizcode = _moduleConfig.typeRow.bizcode;
					var uploadParam = {
								data:{
									bizno:record.id,
									bizcode:bizcode
								},
								rootPath:rootPath							
							}
					uploadfiles.imgShow(uploadParam)
				}
			}
		}
			
		
		return public_method;
			
})